package com.i2bt.app.set;

import com.i2bt.common.AndroidRestContract;
import com.i2bt.common.UserConfig;

import com.i2bt.R;
import com.i2bt.app.BaseActivity;
import com.i2bt.app.set.model.GetHomeBoxListJsonHandle;
import com.i2bt.app.set.model.SetAuthorityJsonHandle;
import com.i2bt.bean.AuthoritySetBean;
import com.i2bt.view.SwitchButton;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AuthoritySetActivity extends BaseActivity {
	private ListView list;
	private Button save;
	public SwitchButton switchBtu[];
	GetHomeBoxListJsonHandle json;

	@Override
	@SuppressLint("NewApi")
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.activity_set_authority);
		initView();
		json = new GetHomeBoxListJsonHandle(this);
		json.postTo(AndroidRestContract.REST_AuthoritySet, null);
	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		if (arg0 == save) {
			logic();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub

		boolean result = super.onCreateOptionsMenu(menu);
		this.setActionMenuGroupVisible(false);
		this.setActionBarBack("设置");
		return result;
	}

	@Override
	protected void initView() {
		// TODO Auto-generated method stub
		list = (ListView) this.findViewById(R.id.set_authority_list);
		save = (Button) this.findViewById(R.id.set_authority_save);
		save.setOnClickListener(this);
	}

	public void setList(String txt[], boolean on[]) {
		ArrayList<HashMap<String, Object>> listItems = new ArrayList<HashMap<String, Object>>();
		for (int i = 0; i < txt.length; i++) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("id", txt[i]);
			map.put("on", on[i]);
			listItems.add(map);
		}
		ListViewAdapter listItemAdapter = new ListViewAdapter(this, listItems,
				R.layout.set_authority_item, new String[] { "id", "on" },
				new int[] { R.id.set_authority_item_text,
						R.id.set_authority_item_button });
		list.setAdapter(listItemAdapter);
	}

	@Override
	protected void logic() {
		// TODO Auto-generated method stub
		int length = switchBtu.length;
		String state[] = new String[length];
		for (int i = 0; i < length; i++) {
			if (switchBtu[i].isChecked()) {
				state[i] = "on";
			} else {
				state[i] = "off";
			}
			System.out.println(state[i]);
		}
		AuthoritySetBean bean = new AuthoritySetBean();
		bean.setAct(AndroidRestContract.AUTHORITY_SETTING);
		bean.setToken(UserConfig.token);
		bean.setHomeboxId(json.getHomeBoxId());
		bean.setHomeboxState(state);
		new SetAuthorityJsonHandle(this).postTo(
				AndroidRestContract.REST_SetAuthority, bean);
	}

	class ListViewAdapter extends BaseAdapter {
		private List<? extends Map<String, ?>> mData;
		private int mResource;
		private String[] mFrom;
		private int[] mTo;
		private LayoutInflater mLayoutInflater;
		private boolean btustate[];

		public ListViewAdapter(Context context,
				List<? extends Map<String, ?>> data, int resource,
				String[] from, int[] to) {
			this.mData = data;
			this.mResource = resource;
			this.mFrom = from;
			this.mTo = to;
			this.mLayoutInflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			btustate = new boolean[data.size()];
			for (int i = 0; i < btustate.length; i++) {
				btustate[i] = (Boolean) this.mData.get(i).get(this.mFrom[1]);
			}
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return this.mData.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return this.mData.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View contentView, ViewGroup parent) {
			// TODO Auto-generated method stub
			contentView = this.mLayoutInflater.inflate(this.mResource, parent,
					false);

			TextView textView = (TextView) contentView.findViewById(mTo[0]);
			textView.setText(this.mData.get(position).get(this.mFrom[0])
					.toString());
			textView.setTextSize(15);

			SwitchButton switchbtu = (SwitchButton) contentView
					.findViewById(mTo[1]);
			switchBtu[position] = switchbtu;
			if (btustate[position]) {
				switchBtu[position].setChecked(true);
			} else {
				switchBtu[position].setChecked(false);
			}
			final int k = position;
			switchBtu[position]
					.setOnCheckedChangeListener(new OnCheckedChangeListener() {
						@Override
						public void onCheckedChanged(CompoundButton arg0,
								boolean arg1) {
							// TODO Auto-generated method stub
							btustate[k] = switchBtu[k].isChecked();
						}
					});

			return contentView;
		}
	}

	@Override
	public void backHomeOperate() {
		// TODO Auto-generated method stub
		finish();
	}
}
